package com.hb.domain.user.entity;

import cn.hutool.core.lang.Validator;
import com.hb.core.constant.Constants;
import com.hb.core.enums.CommCode;
import com.hb.core.exception.ServiceException;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @description:
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/9/2 10:04
 * @version:1.0
 */
@Data
public class UserDo {

    private String id;

    /*
        用户名
     */
    private String name;

    /*
        登录名
     */
    private String loginName;

    /*
        密码
     */
    private String password;

    /*
        昵称
     */
    private String nickName;

    /*
        生日
     */
    private Date birthday;

    /*
        性别
     */
    private Integer sex;

    /*
        手机号
     */
    private String phone;

    /*
        身份证号
     */
    private String idCard;

    /*
        邮箱
     */
    private String email;

    /*
        是否启用/禁用
     */
    private Integer enabled;

    public UserDo update() {
        if (StringUtils.isBlank(this.id)) {
            throw new ServiceException(CommCode.PARAM_ERROE, "缺少id");
        }
        if (Constants.SUPER_USER_ID.equals(this.id)) {
            throw new ServiceException(CommCode.PARAM_ERROE, "禁止对超级用户进行编辑");
        }
        return this;
    }

    /*
        启用
     */
    public UserDo enbale() {
        if (Constants.SUPER_USER_ID.equals(this.id)) {
            throw new ServiceException(CommCode.PARAM_ERROE, "禁止对超级用户进行编辑");
        }
        this.setEnabled(Constants.Flag.IS_TRUE);
        return this;
    }

    /*
        禁用
     */
    public UserDo disable() {
        if (Constants.SUPER_USER_ID.equals(this.id)) {
            throw new ServiceException(CommCode.PARAM_ERROE, "禁止对超级用户进行编辑");
        }
        this.setEnabled(Constants.Flag.IS_FLASE);
        return this;
    }

    public void setPhone(String phone) {
        if (StringUtils.isNotBlank(phone) && !Validator.isMobile(phone)) {
            throw new ServiceException(CommCode.PARAM_ERROE, "手机号格是不正确");
        }
        this.phone = phone;
    }

    public void setPassword(String password) {
        if (StringUtils.isNotEmpty(password)) {
            password = DigestUtils.md5Hex(password);
        }
        this.password = password;
    }

}
